import json
from flask import Flask, request, render_template, session, redirect, jsonify
import re
import csv
import pandas as pd
import recommend.recommend_algorithm_my as itemCF

#from myutils.query import querys
from myutils.query import querys
from myutils.homeData import *
from myutils.timeData import *
from myutils.rateData import *
from myutils.addressData import *
from myutils.typeData import *
from myutils.tablesData import *
from myutils.actor import *

import random
app = Flask(__name__)
app.secret_key = 'This is a app.secret_Key , You Know ?'

@app.route('/')
def every():
    return render_template('login.html')

@app.route("/home")
def home():
    email = session['email']
    allData = getAllData()
    maxRate = getMaxRate()
    maxCast = getMaxCast()
    typesAll = getTypesAll()
    maxLang = getMaxLang()
    types = getType_t()
    row,column = getRate_t()
    tablelist = getTableList()
    return render_template(
        "index.html",
        email=email,
        dataLen = len(allData),
        maxRate=maxRate,
        maxCast=maxCast,
        typeLen = len(typesAll),
        maxLang = maxLang,
        types=types,
        row=list(row),
        column=list(column),
        tablelist=tablelist
    )

@app.route("/login",methods=['GET','POST'])
def login():
    if request.method == 'POST':
        request.form = dict(request.form)

        def filter_fns(item):
            return request.form['email'] in item and request.form['password'] in item

        users = querys('select * from user', [], 'select')
        print(users)
        login_success = 0
        for item in users:
            if item[1] == request.form['email'] and item[2] == request.form['password']:
                login_success = 1
        if login_success == 0:
            return '账号或密码错误'
        # login_success = list(filter(filter_fns, users))
        # print(login_success)
        # if not len(login_success):
        #     return '账号或密码错误'

        session['email'] = request.form['email']
        return redirect('/home', 301)

    else:
        return render_template('./login.html')

@app.route("/registry",methods=['GET','POST'])
def registry():
    if request.method == 'POST':
        request.form = dict(request.form)
        if request.form['password'] != request.form['passwordCheked']:
            return '两次密码不符'
        else:
            def filter_fn(item):
                return request.form['email'] in item

            users = querys('select * from user', [], 'select')
            filter_list = list(filter(filter_fn, users))
            if len(filter_list):
                return '该用户名已被注册'
            else:
                querys('insert into user(email,password) values(%s,%s)',
                       [request.form['email'], request.form['password']])

        session['email'] = request.form['email']
        return redirect('/home', 301)

    else:
        return render_template('./register.html')

def update_csv(movie_id, movie_rate, csv_file_path):
    df = pd.read_csv(csv_file_path)
    # 查找用户为0且电影ID匹配的行
    mask = (df['user'] == 0) & (df['movie_id'] == str(movie_id))
    # print(mask)
    matched_rows = df.loc[mask]
    # 如果有匹配的行，则更新rate
    if not matched_rows.empty:
        df.loc[mask, 'rate'] = movie_rate
        print(f"Updated rate for movie_id {movie_id} to {movie_rate}")
    else:
        # 如果没有匹配的行，则添加新行
        new_row = {'user': 0, 'movie_id': str(movie_id), 'rate': movie_rate}
        df = df.append(new_row, ignore_index=True)
        print(f"Added new row for movie_id {movie_id} with rate {movie_rate}")
        # 可选：将更新后的DataFrame写回CSV文件
    df.to_csv(csv_file_path, index=False)

@app.route("/recommend", methods=['GET', 'POST'])
def recommend():
    email = session['email']
    allData = getAllData()  # getAlldata（）中获取所以movies信息
    data = []  # 用来存储与搜索关键词匹配的数据
    if request.method == 'GET':
        return render_template(
            'recommend.html',
            idData=data,
            email=email,
            )
    else:
        movie_id = request.form.get('searchIpt')
        movie_rate = request.form.get('Ipt_rating')
        print(movie_id, movie_rate)
        if movie_id and movie_rate:
            csv_file_path = 'recommend/ratings_sorted.csv'
            update_csv(movie_id, movie_rate, csv_file_path)
        return render_template(
            'recommend_result.html',
            email=email,
            message="Your rating has been updated or added."
        )

@app.route("/recommend_result", methods=['GET', 'POST'])
def recommend_result():
    allData = getAllData()  # getAlldata（）中获取所以movies信息
    data = []  # 用来存储与搜索关键词匹配的数据
    if request.method == 'GET':
        item = itemCF.ItemBasedCF()
        item.get_dataset()
        item.calc_movie_sim()
        result = item.evaluate()
        with open('recommend/rec_result.csv', 'w', newline='') as f:
            writer = csv.writer(f)
            for row in result.items():
                writer.writerow(row)
        with open('recommend/rec_result.csv', 'r') as f_result:
            movie_result = csv.reader(f_result)
            first_elements = set(row[0] for row in movie_result)
            first_element_list = list(first_elements)
            data_result = []
            for searchWord in first_element_list:
                print(searchWord)
                modified_string = searchWord.replace("'", "")
                print(modified_string)
                def filter_fn(item):
                    # print(item[3])
                    if item[3].find(modified_string) == -1:
                        return False
                    else:
                        return True
                data = list(filter(filter_fn, allData))
                print(data)
                data_result.append(data)
        return render_template(
            'recommend_result1.html',
            result=data_result
        )

@app.route("/search/<int:searchId>",methods=['GET','POST'])
def search(searchId):   #searchID 为传入的搜索关键词
    email = session['email']
    allData = getAllData()  #getAlldata（）中获取所以movies信息
    data = []       #用来存储与搜索关键词匹配的数据
    if request.method == 'GET':
        if searchId == 0:
            return render_template(
                'search.html',
                idData=data,
                email=email
            )

        for i in allData:
            if i[0] == searchId:  #从数据库中进行匹配 搜索ID
                data.append(i)
        return render_template(
                'search.html',
                data=data,
                email=email
            )
    else:
        searchWord = dict(request.form)['searchIpt']
        def filter_fn(item):
            if item[3].find(searchWord) == -1:
                return False
            else:
                return True
        data = list(filter(filter_fn, allData))
        return render_template(
            'search.html',
            data=data,
            email=email
        )

@app.route("/time_t",methods=['GET','POST'])
def time_t():
    email = session['email']
    row,column = getTimeList()
    moveTimeData = getMovieTimeList()
    return render_template(
        'time_t.html',
        email=email,
        row=list(row),
        column=list(column),
        moveTimeData=moveTimeData
    )

@app.route("/rate_t/<type>",methods=['GET','POST'])
def rate_t(type):
    email = session['email']
    typeAll = getTypesAll()
    rows,columns = getMean()
    x,y,y1 = getCountryRating()
    if type == 'all':
        row, column = getRate_t()
    else:
        row,column = getRate_tType(type)
    if request.method == 'GET':
        starts,movieName = getStart('长津湖')
    else:
        searchWord = dict(request.form)['searchIpt']
        starts,movieName = getStart(searchWord)
    return render_template(
        'rate_t.html',
        email=email,
        typeAll=typeAll,
        type=type,
        row=list(row),
        column=list(column),
        starts=starts,
        movieName=movieName,
        rows = rows,
        columns = columns,
        x=x,
        y=y,
        y1=y1
    )

@app.route("/address_t",methods=['GET','POST'])
def address_t():
    email = session['email']
    row,column = getAddressData()
    rows,columns = getLangData()
    return render_template('address_t.html',row=row,column=column,rows=rows,columns=columns,email=email)

@app.route('/type_t',methods=['GET','POST'])
def type_t():
    email = session['email']
    result = getMovieTypeData()
    return render_template('type_t.html',result=result,type_t=type_t,email=email)

@app.route('/actor_t')
def actor_t():
    email = session['email']
    x,y = getAllActorMovieNum()
    x1,y1 = getAllDirectorMovieNum()
    return render_template('actor_t.html',email=email,x=x,y=y,x1=x1,y1=y1)

@app.route("/movie/<int:id>")
def movie(id):
    allData = getAllData()
    idData = {}
    for i in allData:
        if i[0] == id:
            idData = i
    return render_template('movie.html',idData=idData)

@app.route('/tables/<int:id>')
def tables(id):
    if id == 0:
        tablelist = getTableList()
    else:
        deleteTableId(id)
        tablelist = getTableList()
    return render_template('tables.html',tablelist=tablelist)

@app.route('/title_c')
def title_c():
    return render_template('title_c.html')

@app.route('/summary_c')
def summary_c():
    return render_template('summary_c.html')

@app.route('/casts_c')
def casts_c():
    return render_template('casts_c.html')

@app.route('/comments_c',methods=['GET','POST'])
def comments_c():
    email = session['email']
    if request.method == 'GET':
        return render_template('comments_c.html', email=email)
    else:
        searchWord = dict(request.form)['searchIpt']
        randomInt = random.randint(1,10000000)
        get_img('commentContent','./static/4.jpg',f'./static/{randomInt}.jpg',searchWord)
        return render_template('comments_c.html', email=email,imgSrc=f'{randomInt}.jpg')

@app.before_request
def before_requre():
    pat = re.compile(r'^/static')
    if re.search(pat,request.path):
        return
    if request.path == "/login" :
        return
    if request.path == '/registry':
        return
    uname = session.get('email')
    if uname:
        return None

    return redirect("/login")


if __name__ == '__main__':
    app.run()
